package com.bnz.shop.service.impl;

import com.bnz.shop.dao.BrandDao;
import com.bnz.shop.dao.SellerDao;
import com.bnz.shop.entity.*;
import com.bnz.shop.entity.group.Goods;
import com.bnz.shop.service.GoodsDescService;
import com.bnz.shop.service.ItemCatService;
import com.bnz.shop.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.common.utils.PageUtils;
import com.bnz.common.utils.Query;

import com.bnz.shop.dao.GoodsDao;
import com.bnz.shop.service.GoodsService;
import org.springframework.transaction.annotation.Transactional;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    private GoodsDescService goodsDescService;

    @Autowired
    private SellerDao sellerDao;

    @Autowired
    private ItemCatService itemCatService;

    @Autowired
    private BrandDao brandDao;


    @Autowired
    private ItemService itemService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }
/**
 * @author: zhanghao
 * @date: 2023/2/23 15:57
 * @param: [goods]
 * @return: void
 * @功能: 保存商品
 **/
    @Override
    @Transactional
    public void insert(Goods goods) {
        /* 1. 添加spu商品 */
        GoodsEntity goodsEntity = goods.getGoods();
        goodsEntity.setAuditStatus("0");
        this.save(goodsEntity);

        //2.添加商品描述表信息
        GoodsDescEntity goodsDesc = goods.getGoodsDesc();
        goodsDesc.setGoodsId(goodsEntity.getId());

        goodsDescService.save(goods.getGoodsDesc());

        //3.添加sku商品表
        addItemList(goods);
    }

    @Override
    public Goods findById(String id) {
        //3.1 查询goods对象
        GoodsEntity goodsEntity = this.getById(id);
        //3.2 查询goodsDesc对象
        GoodsDescEntity goodsDescEntity = goodsDescService.getById(id);
        //3.3 查询item这个sku商品列表
        List<ItemEntity> itemList = itemService.list(new QueryWrapper<ItemEntity>().eq("goods_id", id));
        //3.4 返回查询到的对象
        return new Goods(goodsEntity,goodsDescEntity,itemList);
    }

    private void addItemList(Goods goods) {
        //3.添加sku商品表
        List<ItemEntity> itemList = goods.getItemList();

        for (ItemEntity itemEntity : itemList) {
            //3.1 设置默认属性
            String category3Id = goods.getGoods().getCategory3Id();
            itemEntity.setGoodsId(goods.getGoods().getId());
            itemEntity.setCategoryid(goods.getGoods().getCategory3Id());
            itemEntity.setStatus("0");
            itemEntity.setCreateTime(new Date());
            itemEntity.setUpdateTime(new Date());
            itemEntity.setTitle(goods.getGoods().getGoodsName());

            //3.2绑定其他字段
            String sellerId = goods.getGoods().getSellerId();
            itemEntity.setSellerId(sellerId);
            // ①根据商家id查询商家名字
//            SellerEntity sellerEntity = sellerDao.selectById(sellerId);
//            itemEntity.setSeller(sellerEntity.getNickName());
            // ② 根据分类id查询分类
            ItemCatEntity itemCatEntity = itemCatService.getById(category3Id);
            itemEntity.setCategory(itemCatEntity.getName());
            // ③ 根据品牌id查询品牌
            String brandId = goods.getGoods().getBrandId();
            System.out.println("brandId = " + brandId);
            BrandEntity brandEntity = brandDao.selectById(brandId);
            itemEntity.setBrand(brandEntity.getName());

            //3.3保存sku商品
            itemService.save(itemEntity);
        }
    }

}